import { Injectable } from '@angular/core';
import { UserAuthService } from '@core/services/user-auth.service';
import { ApiService } from '@core/services/http/api.service';
import { map, mergeMap } from 'rxjs/operators';
import { Observable, of, Subscription } from 'rxjs/index';
import { Promise } from '@ag-grid-community/core';
import { dateFormat } from '../../utils/utils';
import {
  endOfWeek,
  getMonth,
  getQuarter,
  getYear,
  lastDayOfMonth,
  startOfMonth,
  startOfWeek, subDays,
  subWeeks,
} from 'date-fns';

@Injectable({
  providedIn: 'root',
})

export class BiService {

  constructor(private userAuthService: UserAuthService,
              private apiService: ApiService) {
  }

  /** 获取公司 **/
  public getCompany(permitId: string) {
    return new Promise(resolve => {
      this.getOtherCompany(permitId).pipe(
        map((body) => {
          return body;
        }),
        mergeMap((data) => this.getAllCompany(data)),
      ).subscribe(body => {
        const companyClone: any[] = [];
        for (const item of body) {
          if (item.operation === '直营' || item.operation === '合伙人' || item.operation === '赋能店') {
            companyClone.push(item);
          }
        }
        resolve(companyClone);
      });
    });
  }

  private getAllCompany(data): Observable<any> {
    if (JSON.stringify(data.permitPurposeSearch.pt_company) === '[]') {
      return this.apiService.post('cm.company.findAllCompany', {}).pipe(map(
        body => {
          if (body) {
            return body['pt_company'];
          }
        }));
    } else {
      return of(data.permitPurposeSearch.pt_company);
    }
  }

  private getOtherCompany(permitId): Observable<any> {
    return this.apiService.post('aut.localPermit.findOneForUi', { 'body': JSON.stringify({ id: permitId }) })
      .pipe(map(body => {
        return body;
      }));
  }

  /** 转换月份 **/
  public conversionMonth(date) {
    let res;
    if (date == undefined) {
      res = new Date();
    } else {
      res = date;
    }
    return res;
  }

  public conversionMonths(date, month?): void {
    let defaultMonth = month ? month : 12;
    let dateItem;
    if (date == undefined || date.length == 0) {
      let defaultDate = new Date();
      let nowMonth = defaultDate.getMonth(); //当前月
      let nowYear = defaultDate.getFullYear(); //当前年
      let startDate: Date;
      if (nowMonth < 12) {
        startDate = new Date(nowYear, nowMonth - defaultMonth + 1, 1);
      } else {
        startDate = new Date(nowYear, nowMonth, 1);
      }

      let endDate: Date = new Date(nowYear, nowMonth + 1, 0, 23, 59, 59);
      dateItem = [
        dateFormat('yyyy-MM-ddT', startDate) + '00:00:00.000Z',
        dateFormat('yyyy-MM-ddT', endDate) + '23:59:59.000Z',
      ];
    } else {
      dateItem = [
        dateFormat('yyyy-MM-ddT', date[0]) + '00:00:00.000Z',
        dateFormat('yyyy-MM-ddT', date[1]) + '23:59:59.000Z',
      ];
    }
    return dateItem;
  }

  public currentMonthsList(date) {
    let dateItem;
    let nowMonth = date.getMonth() + 1; //当前月
    let nowYear = date.getFullYear(); //当前年
    let startDate: Date;
    startDate = new Date(nowYear, nowMonth - 1, 1);
    let endDate: Date = new Date(nowYear, nowMonth, 0, 23, 59, 59);
    dateItem = [
      dateFormat('yyyy-MM-ddT', startDate) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', endDate) + '23:59:59.000Z',
    ];
    return dateItem;
  }

  public currentMonths(date) {
    let nowMonth = date.getMonth() + 1; //当前月
    let nowYear = date.getFullYear(); //当前年
    if (nowMonth < 10) {
      nowMonth = '0' + nowMonth;
    }
    let nowDate = nowYear + '-' + nowMonth;
    return nowDate;
  }

  public getThisYearItem() {
    let dateItem;
    let nowYear: any = getYear(new Date());
    let startDate: Date = new Date(nowYear, 0);
    let endDate: Date = new Date(nowYear, 12, 1, 0, 0, 0, -1);
    dateItem = [
      dateFormat('yyyy-MM-ddTHH:mm:ss', startDate) + '.000Z',
      dateFormat('yyyy-MM-ddTHH:mm:ss', endDate) + '.000Z',
    ];
    return dateItem;
  }

  public getThisHalfYearItem() {
    let dateItem;
    let nowYear: any = getYear(new Date());
    let nowMonth: any = getMonth(new Date());
    let startMonth: any;
    let endMonth: any;
    if (nowMonth < 6) {
      startMonth = 0;
      endMonth = 6;
    } else {
      startMonth = 6;
      endMonth = 12;
    }
    let startDate: Date = new Date(nowYear, startMonth);
    let endDate: Date = new Date(nowYear, endMonth, 1, 0, 0, 0, -1);
    dateItem = [
      dateFormat('yyyy-MM-ddTHH:mm:ss', startDate) + '.000Z',
      dateFormat('yyyy-MM-ddTHH:mm:ss', endDate) + '.000Z',
    ];
    return dateItem;
  }

  public getThisQuarterItem() {
    let dateItem;
    let nowYear: any = getYear(new Date());
    let nowQuarter: any = getQuarter(new Date());
    let startQuarter;
    let endQuarter;
    switch (nowQuarter) {
      case 1 :
        startQuarter = 0;
        endQuarter = 3;
        break;
      case 2:
        startQuarter = 3;
        endQuarter = 6;
        break;
      case 3:
        startQuarter = 6;
        endQuarter = 9;
        break;
      case 4:
        startQuarter = 9;
        endQuarter = 12;
        break;
      default:
    }
    let startDate: Date = new Date(nowYear, startQuarter);
    let endDate: Date = new Date(nowYear, endQuarter, 1, 0, 0, 0, -1);
    dateItem = [
      dateFormat('yyyy-MM-ddTHH:mm:ss', startDate) + '.000Z',
      dateFormat('yyyy-MM-ddTHH:mm:ss', endDate) + '.000Z',
    ];
    return dateItem;
  }

  public getLastMonthItem() {
    let dateItem;
    let nowYear: any = getYear(new Date());
    let nowMonth: any = getMonth(new Date());
    let startDate: Date = new Date(nowYear, nowMonth - 1);
    let endDate: Date = new Date(nowYear, nowMonth, 1, 0, 0, 0, -1);
    dateItem = [
      dateFormat('yyyy-MM-ddT', startDate) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', endDate) + '23:59:59.000Z',
    ];
    return dateItem;
  }

  public getThisMonthItem() {
    let dateItem;
    let startDate: any = startOfMonth(new Date());
    let endDate: any = lastDayOfMonth(new Date());
    dateItem = [
      dateFormat('yyyy-MM-ddT', startDate) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', endDate) + '23:59:59.000Z',
    ];
    return dateItem;
  }

  public getLastWeekItem() {
    let dateItem;
    let lastWeek: any = subWeeks(new Date(), 1);
    let lastStartWeek: any = startOfWeek(new Date(lastWeek), { weekStartsOn: 1 });
    let lastEndWeek: any = endOfWeek(new Date(lastWeek), { weekStartsOn: 1 });
    dateItem = [
      dateFormat('yyyy-MM-ddT', lastStartWeek) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', lastEndWeek) + '23:59:59.000Z',
    ];
    return dateItem;
  }

  public getNearly7DaysItem() {
    let dateItem;
    let nowDay: any = new Date();
    let nearlyDay: any = subDays(new Date(), 6);
    dateItem = [
      dateFormat('yyyy-MM-ddT', nearlyDay) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', nowDay) + '23:59:59.000Z',
    ];
    return dateItem;
  }

  public getNowDayItem() {
    let dateItem;
    let nowDay: any = new Date();
    dateItem = [
      dateFormat('yyyy-MM-ddT', nowDay) + '00:00:00.000Z',
      dateFormat('yyyy-MM-ddT', nowDay) + '23:59:59.000Z',
    ];
    return dateItem;
  }
}
